package mcdevitt.id.projects;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class BlackJack {
	private Scanner input = new Scanner(System.in);
	private ArrayList<Card> playersCards = new ArrayList<Card>();
	private ArrayList<Card> dealersCards = new ArrayList<Card>();
	private DeckOfCards deck = new DeckOfCards();
	private boolean playersTurn = true;
	public static final int WIN=1;
	public static final int LOSS=2;
	public static final int DRAW=3;
	
//prints card to console
	public void printCards(ArrayList<Card> cards) {
		for (int i = 0; i < cards.size(); i++) {
			System.out.print(cards.get(i).getFace());
			System.out.print(cards.get(i).getSuit());
			System.out.print(" ");
		}

	}
//checks state of game
	public int checkCards(int dealerHand, int playerHand) {
		int outcome = 3;
		try {
			Thread.sleep(700);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (dealerHand > 21) {

			System.out.println("Dealer Bust! You Win!");
			outcome=WIN;
			

		} else if (dealerHand > playerHand) {

			System.out.println("You Lose.");
			outcome=LOSS;
			

		} else if (dealerHand == playerHand) {

			System.out.println("Push");
			Game blackJack = new Game();
			blackJack.start();
		}
		return outcome;
	}
//calculate hand value
	public int calculateHand(ArrayList<Card> cards) {
		int totalValue = 0;
		Stack<Card> hand = new Stack<Card>();
		Stack<Card> ace = new Stack<Card>();
		Stack<Card> other = new Stack<Card>();
		hand.addAll(cards);
		while (hand.size() > 0) {
			Card card = hand.pop();
			String face = card.getFace();
			if (face == "A") {
				ace.push(card);
			} else {
				other.push(card);
			}
		}
		while (other.size() > 0) {
			Card otherCard = other.pop();
			totalValue = totalValue + otherCard.getTrueValue(totalValue);
		}
		while (ace.size() > 0) {
			Card aceCard = ace.pop();
			totalValue = totalValue + aceCard.getTrueValue(totalValue);
		}
		return totalValue;
	}
//dealer will hit if his hand is losing
	public int dealerAI(int dealerHand) {

		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Card dealerHit = deck.dealCard();
		dealerHand = dealerHand + dealerHit.getTrueValue(dealerHand);
		dealersCards.add(dealerHit);
		System.out.print(dealerHit.getFace());
		System.out.print(dealerHit.getSuit());
		System.out.print(" ");
		return dealerHand;

	}
//prints the options hit or stay
	public void printOptions() {
		System.out.println(" ");
		System.out.println("Options: 1.Hit  2.Stay");
		System.out
				.print("Please input the number of the option you would like to choose:");

	}
//check if player has Black Jack
	public int checkBlackJack(Card card1, Card card2) {
		String face1 = card1.getFace();
		String face2 = card2.getFace();
		int outcome = 3;
		if (face1 == "A" && (face2 == "J" || face2 == "Q" || face2 == "K")) {
			System.out.println("BlackJack! You Win!");
			outcome=WIN;
			playersTurn = false;
		} else if (face2 == "A"
				&& (face1 == "J" || face1 == "Q" || face1 == "K")) {
			System.out.println("BlackJack! You Win!");
			outcome=LOSS;
			playersTurn = false;
		}
		return outcome;

	}
	
	

	public int play() {
		int handValue = 0;
		int dealersHandValue = 0;
		int outcome=3;

		System.out.println("Player's Cards:");
		Card playerCard1 = deck.dealCard();
		handValue = handValue + playerCard1.getTrueValue(handValue);
		playersCards.add(playerCard1);
		Card playerCard2 = deck.dealCard();
		handValue = handValue + playerCard2.getTrueValue(handValue);
		playersCards.add(playerCard2);
		printCards(playersCards);
		checkBlackJack(playerCard1, playerCard2);

		while (playersTurn) {
			printOptions();
			Game myTest = new Game();
			int userOptionSelection = myTest.getValidOptionSelection(input);
			switch (userOptionSelection) {
			// hit
			case 1:
				Card playerHit = deck.dealCard();
				handValue = handValue + playerHit.getTrueValue(handValue);
				playersCards.add(playerHit);
				printCards(playersCards);
				handValue = calculateHand(playersCards);
				if (handValue > 21) {

					System.out.println("Bust! You Lose.");
					outcome=LOSS;
					playersTurn=false;

					

				}

				break;
			// stay
			case 2:

				System.out.println(" ");
				System.out.println("Dealer's Cards");
				Card dealerCard1 = deck.dealCard();
				dealersHandValue = dealersHandValue
						+ dealerCard1.getTrueValue(dealersHandValue);
				dealersCards.add(dealerCard1);
				Card dealerCard2 = deck.dealCard();
				dealersHandValue = dealersHandValue
						+ dealerCard2.getTrueValue(dealersHandValue);
				dealersCards.add(dealerCard2);
				printCards(dealersCards);
				while (dealersHandValue < handValue) {

					dealersHandValue = dealerAI(dealersHandValue);
					dealersHandValue = calculateHand(dealersCards);

				}
				outcome=checkCards(dealersHandValue, handValue);
				playersTurn=false;
				break;
			}
		}
		return outcome;

	}
}
